/*************************************************************************
    > File Name: driver_gem_test.c
    > Author: baiy
    > Mail: baiyang0223@163.com 
    > Created Time: 2020-10-26-14:50:44
    > Func: 
 ************************************************************************/

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/version.h>


// private header
#include <drm/drmP.h>
#include <drm/drm_gem_cma_helper.h>
#include <drm/drm_mm.h>
#include <linux/miscdevice.h>

#define DEV_NAME "MISC_DRMMM_TEST"
#define BUF_SIZE (0x10240)

static struct drm_device * pdrm;
static struct drm_device drm;

static const struct file_operations mygem_fops = {
        .owner = THIS_MODULE,
        .open = drm_open,
        .release = drm_release,
        .unlocked_ioctl = drm_ioctl,
        .poll = drm_poll,
        .read = drm_read,
        .mmap = drm_gem_cma_mmap,
};

static struct drm_driver mygem_driver = {
        .driver_features        = DRIVER_GEM,
        .fops                   = &mygem_fops,

        .dumb_create            = drm_gem_cma_dumb_create,
        .gem_vm_ops             = &drm_gem_cma_vm_ops,
        .gem_free_object_unlocked = drm_gem_cma_free_object,

        .name                   = "my-gem",
        .desc                   = "My GEM Driver",
        .date                   = "20200601",
        .major                  = 1,
        .minor                  = 0,
};

static int __init mygem_init(void)
{
	pr_info("init [E]\n");
#if 0
	pdrm = drm_dev_alloc(&mygem_driver, NULL);
	if (IS_ERR(pdrm))
		return PTR_ERR(pdrm);
	
	drm_dev_register(pdrm, 0);
#endif
	drm_dev_init(&drm, &mygem_driver, NULL);
	pr_info("init [X]\n");

    return 0;
}

static void __exit mygem_exit(void)
{
    pr_info("exit [E]\n");
#if 0
	drm_dev_unregister(pdrm);
	// drm_dev_release(&pdrm->ref);
	drm_dev_put(pdrm);
#endif
    pr_info("exit [X]\n");
}

module_init(mygem_init);
module_exit(mygem_exit);

MODULE_LICENSE("GPL v2");
